const order = require('../../services/order.js');
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum: 1,
        pageSize: 4,
        down: false,
        list: [],
        select: '0'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.findList()
    },

    findList: function () {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        let that = this
        let data = {
            status: this.data.select,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
        }
        order.list(data, (res) => {
            if (res.code === 0) {
                let l = that.data.list
                let newRes = l.concat(res.data.data)
                that.setData({
                    list: newRes,
                    pageNum: that.data.pageNum + 1
                })
                // 可下拉
                this.setData({
                    down: res.data.isHasNextPage
                })
            }
            wx.hideLoading()
        })
    },

    toDetail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../odetail/odetail?id=' + id
        })
    },

    toDetail1(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../detail/detail?id=' + id
        })
    },

    chooseAddress() {
        const that = this
        wx.chooseAddress({
            success(res) {
                that.setData({
                    address: res,
                })
            }
        })
    },

    /**
     * 上拉触底时触发
     */
    bindscrolltolower: function () {
        if (this.data.down) {
            this.setData({
                down: false
            })
            this.findList()
        }
    },

    changeSelect(e) {
        this.setData({
            select: e.currentTarget.dataset.select,
            list: [],
            pageNum: 1
        })
        this.findList()
    },

    deleteIt(e) {
        const id = e.currentTarget.dataset.id
        let list = this.data.list
        const that = this
        wx.showModal({
            title: '是否删除',
            success(res) {
                if (res.confirm) {
                    const data = {
                        id: id
                    }
                    order.del(data, (res) => {
                        if (res.code === 0) {
                            list.forEach(function (item, i, array) {
                                if (item.id === id) {
                                    array.splice(i, 1)
                                }
                            })
                            that.setData({
                                list: list
                            })
                        }
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
